﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DataProvider;

namespace SGBDSecurity
{
    public partial class ModifyRows : Form
    {

        UserDataProvider aUserDataProvider;
        User aUser;
        Table aTable;
        TableData aTableData;


        public ModifyRows(User iUser, Table iTable)
        {
            aTable = iTable;
            aUser = iUser;
            aUserDataProvider = new UserDataProvider();
            InitializeComponent();
            this.lNome.Text = iUser.Login;
            this.lNivel.Text = iUser.Classe + "";
            this.RefreshTable(true);


        }

        public void RefreshTable(bool iBool)
        {
            this.dataGridView1.Rows.Clear();
            aTableData = aUserDataProvider.GetViewTable2(aTable);
            if (iBool)
            {

                foreach (string temp in aTableData.Colunas)
                {
                    DataGridViewColumn column = new DataGridViewColumn();
                    column.HeaderText = temp;
                    column.CellTemplate = new DataGridViewTextBoxCell();

                    this.dataGridView1.Columns.Add(column);
                }
            }
            for (int j = 0; j < aTableData.Valores.Count / aTableData.Colunas.Count; j++)
            {
                bool aPodeMostrar = true;
                List<string> aListaTemp = new List<string>();
                aListaTemp = aTableData.Valores.GetRange(j * aTableData.Colunas.Count, aTableData.Colunas.Count);
                this.dataGridView1.Rows.Add(aListaTemp.ToArray());
                if (aTable.STipoSeguranca.Equals("campo"))
                {
                    for (int i = 1; i < this.aTableData.Colunas.Count; i = i + 2)
                    {
                        if (!aListaTemp[i].Equals(""))
                        {
                            int aTempInt = int.Parse(aListaTemp[i]);
                            if (aTempInt > aUser.Classe)
                            {
                                this.dataGridView1.Rows[j].Cells[i].ReadOnly = true;
                                this.dataGridView1.Rows[j].Cells[i].Value = "-NoUpdate-";
                                this.dataGridView1.Rows[j].Cells[i - 1].ReadOnly = true;
                                this.dataGridView1.Rows[j].Cells[i - 1].Value = "-NoUpdate-";
                            }
                            else if (aTempInt < aUser.Classe)
                            {
                                this.dataGridView1.Rows[j].Cells[i].ReadOnly = true;
                                this.dataGridView1.Rows[j].Cells[i-1].ReadOnly = true;
                            }
                        }
                    }
                }
                else if (aTable.STipoSeguranca.Equals("registro"))
                {
                    int aTempInt = int.Parse(aListaTemp[this.aTableData.Colunas.Count - 1]);
                    if (aTempInt != aUser.Classe)
                    {
                        aPodeMostrar = false;
                        for (int i = 0; i < aTableData.Colunas.Count; i++)
                        {
                            aTableData.Valores.RemoveAt(j * aTableData.Colunas.Count);
                        }
                        j--;

                    }
                }
                else if (aTable.STipoSeguranca.Equals("tabela"))
                {
                    if (aTable.Classe != aUser.Classe)
                    {
                        aPodeMostrar = false;
                    }
                }
                if (!aPodeMostrar)
                {
                    this.dataGridView1.Rows.RemoveAt(this.dataGridView1.Rows.Count - 1);
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.lblMessage.Text = "";


                if (this.dataGridView1.SelectedRows.Count > 0)
                {
                    string aInsert = this.VerifyIntegrity();

                    if (!aInsert.Equals(""))
                    {
                        this.DeleteRow();

                        this.InsertRow(aInsert);
                        PopUp aPop = new PopUp("Row modified!");
                        aPop.Visible = true;
                        this.RefreshTable(false);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMessage.Text = ex.Message;
            }

        }


        public void DeleteRow() {

            List<string> valores = new List<string>();
            if (aTable.STipoSeguranca.Equals("campo"))
            {
               
                valores.Add(aTableData.Colunas[0]);
                valores.Add(aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count]);
                valores.Add(aTableData.Colunas[1]);
                valores.Add(aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count + 1]);
                
            }
            else if (aTable.STipoSeguranca.Equals("registro"))
            {
                
                valores.Add(aTableData.Colunas[0]);
                valores.Add(aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count]);
                valores.Add(aTableData.Colunas[aTableData.Colunas.Count - 1]);
                valores.Add(aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count + aTableData.Colunas.Count - 1]);
                
            }
            else
            {
                valores.Add(aTableData.Colunas[0]);
                valores.Add(aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count]);
                valores.Add("0");
                valores.Add("0");
            }
            aUserDataProvider.DeleteTable(aTable, valores);
        }

        public string VerifyIntegrity() {
            string valores = "";
            string temp = "";
            for (int i = 0; i < this.aTableData.Colunas.Count; i++)
            {
                temp = "";
                if (aTable.STipoSeguranca.Equals("campo") && i == ((i / 2) * 2) && (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value != null && !dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value.ToString().Equals("")) && dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i + 1].Value == null)
                {

                    PopUpSecurityLevel aPop = new PopUpSecurityLevel(aTableData.Colunas[i + 1]);
                    aPop.Visible = true;
                    return "";

                }
                if (aTableData.TipoColuna[i] == "System.String")
                {
                    if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].ReadOnly != true)
                    {

                        if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value != null && !dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value.ToString().Equals(""))
                        {
                            valores += "'" + dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value.ToString() + "'";
                        }
                        else valores += "NULL";
                    } else {
                        if (!aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count + i].Equals(""))
                        {
                            valores += "'" + aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count + i] + "'"; 
                        } else {
                            valores += "NULL";                            
                        }
                    }

                        
                }
                else if (aTableData.TipoColuna[i] == "System.Int32")
                {
                    if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value != null && !dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value.ToString().Equals(""))
                        temp = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value.ToString();
                    else temp = "NULL"; 
                    try
                    {
                        if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].ReadOnly != true)
                        {
                            int aTempInt = -1;
                            if (!temp.Equals("NULL")) aTempInt = int.Parse(temp);
                            valores += temp;
                            if ((aTable.STipoSeguranca.Equals("campo") && i != ((i / 2) * 2) && aTempInt != -1) || (aTable.STipoSeguranca.Equals("registro") && i == this.aTableData.Colunas.Count - 1))
                            {
                                if (aTempInt < aUser.Classe || aTempInt > 4)
                                {
                                    PopUpSecurityLevel aPopUp = new PopUpSecurityLevel(aTableData.Colunas[i]);
                                    aPopUp.Visible = true;
                                    dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value = string.Empty;
                                    return "";
                                }
                            }
                        }
                        else
                        {
                            valores += aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count + i];
                        }
                        
                    }
                    catch (Exception g)
                    {
                        TypeExcpetion aJanelaException = new TypeExcpetion(aTableData.Colunas[i], aTableData.TipoColuna[i]);
                        aJanelaException.Visible = true;
                        dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value = string.Empty;
                        valores = "";
                        return valores;
                    }

                }
                else if (aTableData.TipoColuna[i] == "System.Decimal")
                {
                    try
                    {
                        if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].ReadOnly != true)
                        {
                            if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value != null && !dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value.ToString().Equals(""))
                            {
                                temp = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value.ToString();
                                decimal.Parse(temp);
                                temp = temp.Replace(",", ".");
                                valores += temp;
                            }
                            else valores += "NULL";
                        }
                        else
                        {
                            temp = aTableData.Valores[dataGridView1.CurrentRow.Index * aTableData.Colunas.Count + i].Replace(",", ".");
                            valores += temp;
                        }
                    }
                    catch (Exception h)
                    {
                        TypeExcpetion aJanelaException = new TypeExcpetion(aTableData.Colunas[i], aTableData.TipoColuna[i]);
                        aJanelaException.Visible = true;
                        dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[i].Value = string.Empty;
                        valores = "";
                        return valores;
                    }
                }
                else
                {
                    valores += "NULL";
                }
                if (i < this.aTableData.Colunas.Count - 1)
                {
                    valores += ",";
                }
            }
            return valores;        
        }


        public void InsertRow(string valores) {            
            aUserDataProvider.InsertTable(aTable, valores);        
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
